/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/component/AbstractComponent.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: AbstractComponent.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.component;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.elancom.framework.component.event.AfterListener;
import com.elancom.framework.component.event.BeforeListener;
import com.elancom.framework.component.event.EventListenerSupport;
import com.elancom.framework.component.internal.DefaultInvocationFactory;
import com.elancom.framework.component.validate.ValidateSupport;
import com.elancom.framework.component.validate.Validator;

/**
 * @author elancom
 * @date 2012-5-27
 */
public abstract class AbstractComponent implements Component, ServiceReceiver, EventListenerSupport, Validator, ValidateSupport, ComponentLifecycle, ComponentConstants {
	private static InvocationFactory invocationFactory = new DefaultInvocationFactory();

	private List<AfterListener> afterListeners = null;

	private List<BeforeListener> beforeListeners = null;

	protected ComponentInvoker componentInvoker;

	protected ComponentRepository componentRepository;

	private InputFactory inputFactory;

	private OutputFactory outputFactory;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	private ServiceReceiver serviceReceiver;

	private Validator validator;

	public AbstractComponent() {
		super();
		System.out.println("组件装配成功：" + this.getClass().getSimpleName());
	}

	public void destroy() {
	}

	@Override
	public Output execute(Input input) throws ComponentException {
		Output output = outputFactory.create();
		invocationFactory.factory(this, this, input, output).invoke();
		return output;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.event.EventListenerSupport#getAfterListeners()
	 */
	@Override
	public List<AfterListener> getAfterListeners() {
		return this.afterListeners;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.event.EventListenerSupport#getBeforeListeners()
	 */
	@Override
	public List<BeforeListener> getBeforeListeners() {
		return this.beforeListeners;
	}

	/**
	 * @return the componentInvoker
	 */
	public ComponentInvoker getComponentInvoker() {
		return componentInvoker;
	}

	/**
	 * @return the componentRepository
	 */
	public ComponentRepository getComponentRepository() {
		return componentRepository;
	}

	/**
	 * @return the serviceReceiver
	 */
	public ServiceReceiver getServiceReceiver() {
		return serviceReceiver == null ? serviceReceiver : this;
	}

	/**
	 * @return the validator
	 */
	public Validator getValidator() {
		return validator;
	}

	public void init() {
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.ServiceReceiver#service(com.elancom.framework.component.Input, com.elancom.framework.component.Output)
	 */
	@Override
	public void service(Input input, Output output) throws ComponentException {
		if (getServiceReceiver() != null && this.serviceReceiver != this) {
			this.serviceReceiver.service(input, output);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.event.EventListenerSupport#setAfterListeners(java.util.List)
	 */
	@Override
	public void setAfterListeners(List<AfterListener> listeners) {
		this.afterListeners = listeners;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.event.EventListenerSupport#setBeforeListeners(java.util.List)
	 */
	@Override
	public void setBeforeListeners(List<BeforeListener> listeners) {
		this.beforeListeners = listeners;

	}

	/**
	 * @param componentInvoker
	 *            the componentInvoker to set
	 */
	public void setComponentInvoker(ComponentInvoker componentInvoker) {
		this.componentInvoker = componentInvoker;
	}

	/**
	 * @param componentRepository
	 *            the componentRepository to set
	 */
	public void setComponentRepository(ComponentRepository componentRepository) {
		this.componentRepository = componentRepository;
	}

	/**
	 * @param serviceReceiver
	 *            the serviceReceiver to set
	 */
	public void setServiceReceiver(ServiceReceiver serviceReceiver) {
		this.serviceReceiver = serviceReceiver;
	}

	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	/*
	 * (non-Javadoc)
	 * @see com.elancom.framework.component.validate.Validator#validate(com.elancom.framework.component.Input, com.elancom.framework.component.Output)
	 */
	@Override
	public void validate(Input input, Output output) throws ComponentException {
	}

	/**
	 * @return the inputFactory
	 */
	public InputFactory getInputFactory() {
		return inputFactory;
	}

	/**
	 * @param inputFactory
	 *            the inputFactory to set
	 */
	public void setInputFactory(InputFactory inputFactory) {
		this.inputFactory = inputFactory;
	}

	/**
	 * @return the outputFactory
	 */
	public OutputFactory getOutputFactory() {
		return outputFactory;
	}

	/**
	 * @param outputFactory
	 *            the outputFactory to set
	 */
	public void setOutputFactory(OutputFactory outputFactory) {
		this.outputFactory = outputFactory;
	}

	protected Input createInput() {
		return inputFactory.create();
	}
}
